﻿#region
/* ===============================================
 * 作者：guopeng           时间：2012/2/1 22:06:03
 * 
 * 
 * 类描述：
 * 
 * 
 * =============================================== */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ueEngine.Framework.Common
{
    public static class TypePlug
    {
        public static int ToInt32(this object o, int defaultVal)
        {
            if (o == null) return defaultVal;
            return o.ToString().ToInt32(defaultVal);
        }
        public static int ToInt32(this string str, int defaultVal)
        {
            int result = 0;
            if (Int32.TryParse(str, out result))
            {
                return result;
            }
            return defaultVal;
        }

        public static decimal ToDecimal(this object o, decimal defaultVal)
        {
            if (o == null) return defaultVal;
            return o.ToString().ToDecimal(defaultVal);
        }
        public static decimal ToDecimal(this string str, decimal defaultVal)
        {
            decimal result = 0m;
            if (decimal.TryParse(str, out defaultVal))
            {
                return result;
            }
            return defaultVal;
        }

        public static DateTime ToDateTime(this object o, DateTime defaultVal)
        {
            if (o == null) return defaultVal;
            return o.ToString().ToDateTime(defaultVal);
        }
        public static DateTime ToDateTime(this string str, DateTime defaultVal)
        {
            DateTime dt;
            if (DateTime.TryParse(str, out dt))
            {
                return dt;
            }
            return defaultVal;
        }
    }
}
